package com.motu.data;

import com.motu.monstercity.module.common.utility.ServerParam;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.json.JsonObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 魔兔BI数据管理类
 */
public class MotuBiManager extends MotuBiBaseManager {

	private static final Logger logger = LoggerFactory.getLogger(MotuBiManager.class);

	// 事件名称
	public static final String EVENT_REGISTER = "register";//注册事件
	public static final String EVENT_LOGIN = "login";//登录事件
	public static final String EVENT_CHARGE = "charge";//充值事件

	/**
	 * 获取公共参数
	 *
	 * @param eventName       事件名称
	 * @param userId          用户ID
	 * @param userLoginDevice 用户登录设备信息
	 * @return 上报公共参数
	 */
	public static JsonObject getCommonParams(String eventName, long userId, UserLoginDevice userLoginDevice) {
		if (ServerParam.SEND_MOTU_BI_DATA == 0) {
			return null;
		}

		if (userLoginDevice == null) {
			logger.error("MotuBiManager send error loginDeviceInfo is null userId:{}", userId);
			return null;
		}

		JsonObject commonParams = new JsonObject();
		commonParams.put("game_id", ServerParam.MOTU_SDK_GAMEID);
		commonParams.put("event_name", eventName);
		commonParams.put("platform", userLoginDevice.getPlatform());
		commonParams.put("channel", userLoginDevice.getChannel());
		commonParams.put("time", System.currentTimeMillis());
		commonParams.put("os", Tool.isEmpty(userLoginDevice.getRyos()) ? "other" : userLoginDevice.getRyos().toLowerCase());
		commonParams.put("ip", userLoginDevice.getIp());
		commonParams.put("device_id", userLoginDevice.getMotuBiDeviceId());
		commonParams.put("account_id", userLoginDevice.getAccountId());
		commonParams.put("uuid", Tool.getUUID());
		commonParams.put("user_id", String.valueOf(userId));
		commonParams.put("server_id", userLoginDevice.getServerId());
		return commonParams;
	}

	/***
	 * 登录事件
	 * @param userId          用户ID
	 * @param userLoginDevice 用户登录设备信息
	 */
	public static void sendLoginEvent(long userId, UserLoginDevice userLoginDevice) {
		JsonObject params = getCommonParams(EVENT_LOGIN, userId, userLoginDevice);
		if (params == null) {
			return;
		}
		params.put("properties", new JsonObject());// 无事件属性
		pushToQueue(params);
	}

	/***
	 * 注册事件
	 * @param userId          用户ID
	 * @param userLoginDevice 用户登录设备信息
	 */
	public static void sendRegisterEvent(long userId, UserLoginDevice userLoginDevice) {
		JsonObject params = getCommonParams(EVENT_REGISTER, userId, userLoginDevice);
		if (params == null) {
			return;
		}
		params.put("properties", new JsonObject());// 无事件属性
		pushToQueue(params);
	}

	/***
	 * 充值事件
	 * @param userId          用户ID
	 * @param userLoginDevice 用户登录设备信息
	 * @param orderId         订单id
	 * @param money           付费金额（人民币：元）
	 * @param moneyEn         付费金额（美元）
	 * @param iapGiftId       礼包ID
	 * @param iapGiftType     礼包类型
	 * @param payType         支付类型
	 */
	public static void sendChargeEvent(long userId, UserLoginDevice userLoginDevice, String orderId,
									   double money, double moneyEn, int iapGiftId, int iapGiftType, String payType) {

		JsonObject params = getCommonParams(EVENT_CHARGE, userId, userLoginDevice);
		if (params == null) {
			return;
		}

		// 充值事件公共参数中的uuid要定义成订单id
		params.put("uuid", orderId);

		// 定义事件属性
		JsonObject properties = new JsonObject();
		properties.put("rmb_amount", (int) (money * 100));// 上报单位：分
		properties.put("usd_amount", (int) (moneyEn * 100));// 上报单位：美分
		properties.put("order_id", orderId);
		properties.put("gift_id", iapGiftId);
		properties.put("gift_type", iapGiftType);
		properties.put("pay_type", payType);

		params.put("properties", properties);
		pushToQueue(params);
	}

}
